<script setup>
import { reactive } from 'vue'
import { login } from '@/api/user.js'
import { useToken, usePassword } from '@/stores'
import { useRouter } from 'vue-router'
const { getPassword } = usePassword()
const router = useRouter()
defineOptions({
  name: 'LoginIndex'
})
const loginForm = reactive(getPassword() || {
  username: 'demo',
  password: 'zh@hm#23',
  isAgree: false // 是否记住密码
})
const onFinish =async (values) => {
  if(values.isAgree === true) {
    const { setPassword } = usePassword()
    setPassword(loginForm)
  }
  const { setToken } = useToken()
  console.log('values:', values)
  delete values.isAgree
  const res = await login(values)
  setToken(res.data.token)
  console.log(res.data.token)
  router.push('/')
}
</script>
<template>
  <div class="login-container">
    <div class="logo">
      <div class="icon"></div>
    </div>
    <div class="form">
      <h3>智慧园区-登录</h3>
      <a-card class="login-card">
        <!-- 登录表单 -->
        <a-form autocomplete="off" :model="loginForm" @finish="onFinish">
          <a-form-item
            name="username"
            :rules="[{ required: true, message: '用户名不能为空', trigger: 'blur' }]"
          >
            <a-input
              size="large"
              style="border-radius: 7px"
              v-model:value="loginForm.username"
            ></a-input>
          </a-form-item>
          <a-form-item
            name="password"
            :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' }]"
          >
            <a-input-password
              size="large"
              style="border-radius: 7px"
              v-model:value="loginForm.password"
            ></a-input-password>
          </a-form-item>
          <a-form-item name="isAgree">
            <a-checkbox v-model:checked="loginForm.isAgree">记住我</a-checkbox>
          </a-form-item>
          <a-form-item>
            <a-button
              size="large"
              type="primary"
              block
              style="border-radius: 7px"
              html-type="submit"
              >登录</a-button
            >
          </a-form-item>
        </a-form>
      </a-card>
    </div>
  </div>
</template>
<style lang="less">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: url(../../assets/login-bg.svg) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 50px;
    .ant-card {
      width: 320px;
      border: none;
      padding: 0;
    }
    h3 {
      padding-left: 30px;
      font-size: 24px;
    }
  }
}
</style>
